#include "Route.hpp"
#include "ChatSever.hpp"
#include "ThreadPool.hpp"
#include <iostream>

void Usage(std::string proc)
{
    std::cerr << "Usage:" << proc << "+ severport" << std::endl;
}
using task_t = std::function<void()>;
//./udpsever severport
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    EnableConsoleLogStrategy();
    //获取输入的port
    uint16_t port = std::stoi(argv[1]);
    //消息转发功能
    std::unique_ptr<Route> r = std::make_unique<Route>();
    //构建线程池对象
    auto tp = ThreadPool<task_t>::GetInstance();
    //服务器功能
    std::unique_ptr<ChatSever> usv = std::make_unique<ChatSever>(port, 
        [&r, &tp](int sockfd, std::string massage, InetAddr addr){
            //将需要发送的消息打包成一个任务放入线程池中
            //r.get()后跟前者函数需要的参数
            task_t task = std::bind(&Route::RouteMessageToAll, r.get(), sockfd, massage, addr);
            tp->Enqueue(task);
        });
    usv->Init();
    usv->Start();

    return 0;
}